/*
 * @Author: FredWang
 * @Date: 2019-01-21 11:10:00
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2019-02-16 09:43:51
 * @Description: 
 */

import axios from "axios";

import { Message, MessageBox} from "element-ui";
import { ERR_OK } from "./config";

const axiosPost = function(url, data,params) {
 
  if(params){
    url+=`?`;
    for( var key in params){
      if(Array.isArray(params[key])){
        for(var i=0; i<params[key].length; i++){
          url += key + '=' + params[key][i] + '&';
        }
      }else{
        url += key + '=' + params[key] + '&';
      }
    }
  }
  return new Promise((resolve, reject) => {
    let that = this
    axios
      .post(url, data)
      .then(res => {
        if (res && res.status === ERR_OK) { 
          resolve(res);
        } else if (res && res.status === 2000) {
            MessageBox.alert('<div class="message-tip-biaoti"><span class="iconfont icon-info"></span><span class="message-title">提示</span></div><span class="message-cont">' + res.msg + '</span>', '', {
              confirmButtonText: '确定',
              showClose: false,
              dangerouslyUseHTMLString: true
            })
          resolve(res);
        } else if(res && res.status === 403){
          window.location.href = `${location.host}/adminSystem/#/login`
        } else {
          //如果服务端响应失败，则统一返回false
          Message({
            message: res.msg,
            type: "error"
          });
          return false;
        }
      })
      .catch(error => {
        reject(error);
      });
  });
};

export default axiosPost;
